import { _decorator, Component, Animation, Sprite, UITransform, animation, AnimationClip, Vec3, SpriteFrame } from 'cc';
import { DIRECTION_ENUM, ENTITY_STATE_ENUM, PARAMS_NAME_ENUM, DIRECTION_ORDER_ENUM, ENTITY_TYPE_ENUM } from '../Enums';
import { TILE_WIDTH, TILE_HEIGHT } from '../Scripts/Tile/TileManager';
import { IEntity } from '../Levels';
import { StateMachine } from './StateMachine';
import { randomById } from '../Utils';
const { ccclass, property } = _decorator;

@ccclass('EntityManager')
export class EntityManager extends Component {
    /**实体的唯一ID */
    id: string = randomById(10)
    x: number = 0
    y: number = 0

    fsm: StateMachine
    /**玩家的目标朝向 */
    private _direction: DIRECTION_ENUM
    /**玩家的状态 */
    private _state: ENTITY_STATE_ENUM
    /**玩家类型 */
    public _type: ENTITY_TYPE_ENUM

    get direction() {
        return this._direction
    }
    set direction(newDirection: DIRECTION_ENUM) {
        if (this._direction === newDirection) return;
        this._direction = newDirection
        this.fsm.setParams(PARAMS_NAME_ENUM.DIRECTION, DIRECTION_ORDER_ENUM[this._direction])
    }

    get state() {
        return this._state
    }
    set state(newState: ENTITY_STATE_ENUM) {
        if (this._state === newState) return;
        this._state = newState
        this.fsm.setParams(this._state, true)
    }

    async init(params: IEntity) {
        const sprite = this.addComponent(Sprite);
        sprite.sizeMode = Sprite.SizeMode.CUSTOM;

        const transform = this.getComponent(UITransform);
        transform.setContentSize(TILE_WIDTH * 4, TILE_HEIGHT * 4);

        this.x = params.x
        this.y = params.y
        this._type = params.type
        this.direction = params.direction
        this.state = params.state

        // 强制设置玩家永远在最上层
        if (this._type === ENTITY_TYPE_ENUM.PLAYER) {
            this.node.setSiblingIndex(999);
        }
    }

    update() {
        this.node.setPosition(this.x * TILE_WIDTH - TILE_WIDTH * 1.5, -this.y * TILE_HEIGHT + TILE_HEIGHT * 1.5)
    }

    onDestroy() {

    }





}


